package com.quan.hr.web.controller;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.commons.exception.BizException;
import com.quan.commons.vo.R;
import com.quan.commons.core.MyBaseController;
import com.quan.commons.utils.PageUtils;
import com.quan.commons.annotation.OperationLog;

import com.quan.hr.commons.export.HrDeptImportVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import com.quan.hr.commons.vo.HrDeptVo;
import com.quan.hr.entity.HrDept;
import com.quan.hr.service.HrDeptService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 机构部门
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2023-02-18 16:25:22
 */
@Slf4j
@Api(tags = "机构部门")
@RestController
@RequestMapping("/hr/hrdept")
public class HrDeptController extends MyBaseController {

    @Autowired
    private HrDeptService service;

    /**
     * 保存
     * @param vo
     * @return
     */
    @OperationLog(module = "机构部门", title = "新增")
    @PostMapping("save")
    public R save(@Valid @RequestBody HrDeptVo vo) {
        this.service.save(vo);
        return R.ok();
    }

    /**
     * 修改
     * @param vo
     * @return
     */
    @OperationLog(module = "机构部门", title = "修改")
    @PostMapping("update")
    public R update(@Valid @RequestBody HrDeptVo vo) {
        this.service.updateById(vo);
        return R.ok();
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @OperationLog(module = "机构部门", title = "删除")
    @PostMapping("delete")
    public R delete(@RequestBody Long[] ids) {
        if (null != ids && ids.length > 0) {
            this.service.removeByIds(Arrays.asList(ids));
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 多条件查询信息详情
     * @param vo
     * @return
     */
    @OperationLog(module = "机构部门", title = "查询详情")
    @GetMapping("/info")
    public R info(HrDeptVo vo) {
        QueryWrapper<HrDept> queryWrapper = new QueryWrapper<HrDept>(vo);
        HrDept hrDepart =this.service.getOne(queryWrapper);
        return R.ok().data(hrDepart);
    }

    /**
     * 根据ID查询信息详情
     * @param {id}
     * @return
     */
    @OperationLog(module = "机构部门", title = "查询详情")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        HrDept hrDepart =this.service.getInfo(id);
        return R.ok().data(hrDepart);
    }

    /**
     * 列表查询
     * @param vo
     * @return
     */
    @OperationLog(module = "机构部门", title = "查询列表")
    @GetMapping(value = "list")
    public Object list(HrDeptVo vo) {
        List<HrDeptVo> list = this.service.queryList(new PageUtils(request), vo);
        return R.ok().data(list);
    }

    /**
     * 分页查询
     * @param vo
     * @return
     */
    @OperationLog(module = "机构部门", title = "查询列表")
    @GetMapping(value = "page")
    public R queryPage(HrDeptVo vo) {
        PageUtils page = this.service.queryPage(new PageUtils(request), vo);
        return R.ok().data(page);
    }

    /**
     * 树形结构查询
     * @return
     */
    @OperationLog(module = "机构部门", title = "树形结构查询")
    @GetMapping(value = "/listWithTree")
    public R listWithTree() {
        return R.ok().put("data", this.service.listWithTree());
    }

    /**
     * 单文件上传
     * 导入岗位Excel数据
     * @param request
     * @return
     */
    @OperationLog(module = "部门信息", title = "导入部门Excel数据")
    @PostMapping(value = "/importExcelDept")
    public R importExcelPost(@RequestParam(value = "file") MultipartFile multipartFile, HttpServletRequest request) throws Exception {
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();

            ImportParams params = new ImportParams();

            final List<HrDeptImportVo> list = new ExcelImportService().importExcelByIs(inputStream, HrDeptImportVo.class, params, false).getList();

            // 保存数据
            this.service.saveImportData(list);

        } finally {
            IOUtils.closeQuietly(inputStream);
        }

        return R.ok();
    }
}
